﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace PeekService
{
    public partial class TicketBuyInfoService
    {
        /// <summary>
        /// 购票
        /// </summary>
        public bool BuyTicket(TicketBuyInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into TicketBuyInfo(");
            strSql.Append("Code,TicketCode,UserCode,TicketCount,Amount,Remark,State,CreateID,CreateTime,Sort)");
            strSql.Append(" values (");
            strSql.Append("@Code,@TicketCode,@UserCode,@TicketCount,@Amount,@Remark,@State,@CreateID,@CreateTime,@Sort)");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,36),
					new SqlParameter("@TicketCode", SqlDbType.VarChar,36),
					new SqlParameter("@UserCode", SqlDbType.VarChar,36),
					new SqlParameter("@TicketCount", SqlDbType.Int,4),
					new SqlParameter("@Amount", SqlDbType.Decimal,9),
					new SqlParameter("@Remark", SqlDbType.NVarChar,500),
					new SqlParameter("@State", SqlDbType.Int,4),
					new SqlParameter("@CreateID", SqlDbType.VarChar,36),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@Sort", SqlDbType.Int,4)};
            parameters[0].Value = model.Code;
            parameters[1].Value = model.TicketCode;
            parameters[2].Value = model.UserCode;
            parameters[3].Value = model.TicketCount;
            parameters[4].Value = model.Amount;
            parameters[5].Value = model.Remark;
            parameters[6].Value = model.State;
            parameters[7].Value = model.CreateID;
            parameters[8].Value = model.CreateTime;
            parameters[9].Value = model.Sort;

            //更新票务信息表中的数据
            strSql.Append(";update TicketInfo set SoldSeat=SoldSeat+").Append(model.TicketCount).Append(" where TicketCode =").Append(model.TicketCode);
            int rows = DBHelper.ExecuteNonQuery(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(TicketBuyInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update TicketBuyInfo set ");
            strSql.Append("TicketCode=@TicketCode,");
            strSql.Append("UserCode=@UserCode,");
            strSql.Append("TicketCount=@TicketCount,");
            strSql.Append("Amount=@Amount,");
            strSql.Append("Remark=@Remark,");
            strSql.Append("State=@State,");
            strSql.Append("CreateID=@CreateID,");
            strSql.Append("CreateTime=@CreateTime,");
            strSql.Append("Sort=@Sort");
            strSql.Append(" where Code=@Code ");
            SqlParameter[] parameters = {
					new SqlParameter("@TicketCode", SqlDbType.VarChar,36),
					new SqlParameter("@UserCode", SqlDbType.VarChar,36),
					new SqlParameter("@TicketCount", SqlDbType.Int,4),
					new SqlParameter("@Amount", SqlDbType.Decimal,9),
					new SqlParameter("@Remark", SqlDbType.NVarChar,500),
					new SqlParameter("@State", SqlDbType.Int,4),
					new SqlParameter("@CreateID", SqlDbType.VarChar,36),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@Sort", SqlDbType.Int,4),
					new SqlParameter("@Code", SqlDbType.VarChar,36)};
            parameters[0].Value = model.TicketCode;
            parameters[1].Value = model.UserCode;
            parameters[2].Value = model.TicketCount;
            parameters[3].Value = model.Amount;
            parameters[4].Value = model.Remark;
            parameters[5].Value = model.State;
            parameters[6].Value = model.CreateID;
            parameters[7].Value = model.CreateTime;
            parameters[8].Value = model.Sort;
            parameters[9].Value = model.Code;

            int rows = DBHelper.ExecuteNonQuery(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string Code)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from TicketBuyInfo ");
            strSql.Append(" where Code=@Code ");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,36)			};
            parameters[0].Value = Code;

            int rows = DBHelper.ExecuteNonQuery(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        

    }
}
